package com.jhs.boot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jhs.boot.system.model.vo.GraphNodeVO;
import com.jhs.boot.system.model.vo.NodesThirdVO;
import com.jhs.boot.system.service.SysNodesService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhs.boot.system.mapper.SysNodesTrafficMapper;
import com.jhs.boot.system.service.SysNodesTrafficService;
import com.jhs.boot.system.model.entity.SysNodesTraffic;
import com.jhs.boot.system.model.form.SysNodesTrafficForm;
import com.jhs.boot.system.model.query.SysNodesTrafficQuery;
import com.jhs.boot.system.model.vo.SysNodesTrafficVO;
import com.jhs.boot.system.converter.SysNodesTrafficConverter;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 节点流量服务实现类
 *
 * @author zxy
 * @since 2025-08-11 19:21
 */
@Service
@RequiredArgsConstructor
public class SysNodesTrafficServiceImpl extends ServiceImpl<SysNodesTrafficMapper, SysNodesTraffic> implements SysNodesTrafficService {

    private final SysNodesTrafficConverter sysNodesTrafficConverter;
    private final SysNodesService sysNodesService;

    /**
    * 获取节点流量分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<SysNodesTrafficVO>} 节点流量分页列表
    */
    @Override
    public IPage<SysNodesTrafficVO> getSysNodesTrafficPage(SysNodesTrafficQuery queryParams) {
        Page<SysNodesTrafficVO> pageVO = this.baseMapper.getSysNodesTrafficPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取节点流量表单数据
     *
     * @param id 节点流量ID
     * @return 节点流量表单数据
     */
    @Override
    public SysNodesTrafficForm getSysNodesTrafficFormData(Long id) {
        SysNodesTraffic entity = this.getById(id);
        return sysNodesTrafficConverter.toForm(entity);
    }
    
    /**
     * 新增节点流量
     *
     * @param formData 节点流量表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveSysNodesTraffic(SysNodesTrafficForm formData) {
        SysNodesTraffic entity = sysNodesTrafficConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新节点流量
     *
     * @param id   节点流量ID
     * @param formData 节点流量表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateSysNodesTraffic(Long id,SysNodesTrafficForm formData) {
        SysNodesTraffic entity = sysNodesTrafficConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除节点流量
     *
     * @param ids 节点流量ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteSysNodesTraffics(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的节点流量数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public List<NodesThirdVO> getAllNodes(Long id, String timeRange) {
        List<GraphNodeVO> allNodes = sysNodesService.getAllNodes();
        List<Long> idList =new ArrayList<>();
        if (id > 0) {
            idList.add(id);
        }else{
           idList = allNodes.stream().map(GraphNodeVO::getId).collect(Collectors.toList());
        }
        if(idList.size()==0){
            return null;
        }
        LocalDateTime currentTime = LocalDateTime.now();
        LocalDateTime oneHourAgo =null;
        switch (timeRange){
            case "1h":
                  oneHourAgo = currentTime.minusHours(1);
                break;
            case "0.5h":
                oneHourAgo = currentTime.minusMinutes( 30);
                break;
            case "0.1h":
                oneHourAgo = currentTime.minusMinutes( 10);
                break;
            default:
                break;
        }

        List<SysNodesTraffic> list = this.list(new LambdaQueryWrapper<SysNodesTraffic>()
                .in(SysNodesTraffic::getNodeId, idList).ge(oneHourAgo!=null,SysNodesTraffic::getCreateTime,oneHourAgo));

        return sysNodesTrafficConverter.toListEntity(list);
    }

    public static void main(String[] args) {
        LocalDateTime currentTime = LocalDateTime.now();
        System.out.println( currentTime.minusMinutes((long)10));
    }
}
